/**
 * SrvUtilityPortlets -  http://code.google.com/p/alui-toolbox/
 * Copyright (c) 2008-2009
 * by Fabien Sanglier
 * 
 * This file is part of SrvUtilityPortlets.
 * 
 * SrvUtilityPortlets is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * SrvUtilityPortlets is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with SrvUtilityPortlets.  If not, see <http://www.gnu.org/licenses/>.
 * 
 */

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using BEA.ALI.Web.UI;
using com.plumtree.server;
using AluiServerLib.Helper;
using AluiServerLib.Models;
using AluiServerLib.Business;
using com.plumtree.openfoundation.util;

namespace ALUIServerUtilities
{
    public partial class CachingClearer : ALIPortletPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            portletTreeLink.Text = WebHelper.CreateTreePickerTag(PT_CLASSIDS.PT_GADGET_ID, "Pick Portlets", "Portlets", "Pick Portlets", "PortletsPickerSubmit" + m_portletRequest.GetPortletID());
            pageTreeLink.Text = WebHelper.CreateTreePickerTag(PT_CLASSIDS.PT_COMMUNITYPAGES_ID, "Pick Pages", "Pages", "Pick Pages", "PagesPickerSubmit" + m_portletRequest.GetPortletID());
            communityTreeLink.Text = WebHelper.CreateTreePickerTag(PT_CLASSIDS.PT_COMMUNITY_ID, "Pick Communities", "Communities", "Pick Communities", "CommunitiesPickerSubmit" + m_portletRequest.GetPortletID());
            
            if (!Page.IsPostBack)
            {
                //
            }
        }

        protected void cmdSubmit_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                IPTSession session = NativeApplicationMgr.GetInstance().GetNativeSession(m_portletRequest.GetLoginToken());
                PTNavigationModel ptnav = new PTNavigationModel(session);
                PTPortletModel ptportlets = new PTPortletModel(session);

                //find portlets
                ArrayList arrAggregatePortlets = new ArrayList();
                ArrayList arrTempPortlets = null;

                Response.Write("<div>"); 
                Response.Write("<font color=\"green\">Finding Portlets...</font>");
                if (!String.IsNullOrEmpty(txtCommunities.Text.Trim()))
                {
                    Response.Write("<br /><font color=\"green\">...in communities: " + txtCommunities.Text + " </font>");
                    arrTempPortlets = ptportlets.GetPortletsOnCommunities(ArrayHelper.ConvertToIntArray(txtCommunities.Text.Trim().Split(',')), false);
                    if (null != arrTempPortlets)
                    {
                        arrAggregatePortlets.AddRange(arrTempPortlets);
                        Response.Write("<br /><font color=\"green\">Found " + arrTempPortlets.Count + " </font>");
                    }
                    else
                    {
                        Response.Write("<br /><font color=\"green\">Did not find any.</font>");
                    }
                }

                if (!String.IsNullOrEmpty(txtPages.Text.Trim()))
                {
                    Response.Write("<br /><font color=\"green\">...in pages: " + txtPages.Text + " </font>"); 
                    arrTempPortlets = ptportlets.GetPortletsOnPages(ArrayHelper.ConvertToIntArray(txtPages.Text.Trim().Split(',')), false);
                    if (null != arrTempPortlets)
                    {
                        arrAggregatePortlets.AddRange(arrTempPortlets);
                        Response.Write("<br /><font color=\"green\">Found " + arrTempPortlets.Count + " </font>");
                    }
                    else
                    {
                        Response.Write("<br /><font color=\"green\">Did not find any.</font>");
                    }
                }

                if (!String.IsNullOrEmpty(txtPortlet.Text.Trim()))
                {
                    Response.Write("<br /><font color=\"green\">..." + txtPortlet.Text + " </font>"); 
                    arrTempPortlets = ptportlets.GetPortlets(ArrayHelper.ConvertToIntArray(txtPortlet.Text.Trim().Split(',')), false);
                    if (null != arrTempPortlets)
                    {
                        arrAggregatePortlets.AddRange(arrTempPortlets);
                        Response.Write("<br /><font color=\"green\">Found " + arrTempPortlets.Count + " </font>");
                    }
                    else
                    {
                        Response.Write("<br /><font color=\"green\">Did not find any.</font>");
                    }
                }


                if (null != arrAggregatePortlets)
                {
                    Response.Write("<br /><br /><font color=\"green\">Found a Total of " + arrAggregatePortlets.Count + " Portlets ...</font>");
                    Response.Write("</div>");

                    Response.Write("<div>");
                    Response.Write("<font color=\"green\">Clearing Cache Operation</font><br />");
                        
                    foreach (ALUIPortlet portlet in arrAggregatePortlets)
                    {
                        Response.Write("<div>"); 
                        Response.Write("<br /><font color=\"green\">Clearing Cache for Portlet " + portlet.GetObjectID() + "...</font>");

                        try
                        {
                            portlet.Inflate(session, true);
                            portlet.SetLastModified(new XPDateTime());
                            portlet.Store();
                            Response.Write("<br /><font color=\"green\">...Cache for Portlet " + portlet.GetObjectID() + " Successfully Cleared!</font>");
                        }
                        catch (Exception exc)
                        {
                            Response.Write("<br /><font color=\"red\">An error Occured.</font><br />");
                            Response.Write("<br /><font color=\"red\">" + exc + "</font><br />");
                        }
                        finally
                        {
                            if (null != portlet && portlet.GetLockState() == PT_LOCKSTATES.PT_LOCKED)
                                portlet.UnlockObject();
                        }
                        Response.Write("</div>");
                    }
                    Response.Write("<div><font color=\"green\">Operation Successfull!!</font></div>");
                    Response.Write("</div>");
                }
                else
                {
                    Response.Write("<br /><br /><font color=\"green\">Did not find any portlets...</font>");
                    Response.Write("</div>");
                }

                pnlCacheForm.Visible = false;
                Response.Write("<br /><br /><a href=\"pt://return\">Return To Portal</a>");
            }
        }

        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);

            ClientScript.RegisterClientScriptBlock(Page.GetType(), "PortletsPickerSubmit", WebHelper.CreateTreePickerJS("PortletsPickerSubmit" + m_portletRequest.GetPortletID(), txtPortlet.ClientID, ","), true);
            ClientScript.RegisterClientScriptBlock(Page.GetType(), "PagesPickerSubmit", WebHelper.CreateTreePickerJS("PagesPickerSubmit" + m_portletRequest.GetPortletID(), txtPages.ClientID, ","), true);
            ClientScript.RegisterClientScriptBlock(Page.GetType(), "CommunitiesPickerSubmit", WebHelper.CreateTreePickerJS("CommunitiesPickerSubmit" + m_portletRequest.GetPortletID(), txtCommunities.ClientID, ","), true);
        }
    }
}
